package com.snowfeel.sfmall.security.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Sets;
import com.snowfeel.sfmall.security.constants.SecurityConstants;
import com.snowfeel.sfmall.security.vo.SecurityUserVo;
import com.snowfeel.sfmall.security.vo.SysUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Spring Security Context工具类
 * <p>
 * 获取登录用户信息(用户名、角色、部门)
 *
 * @author haoxr
 * @date 2022/10/29
 */
@Component
public class SecurityUtils {

    /**
     * 获取用户ID
     *
     * @return
     */
    public static Long getUserId() {
        SecurityUserVo currentUser = getCurrentUser();
        return Objects.isNull(currentUser) ? null : currentUser.getId();
    }


    /**
     * 获取用户登录名
     *
     * @return username
     */
    public static String getUsername() {
        SecurityUserVo currentUser = getCurrentUser();
        return Objects.isNull(currentUser) ? null : currentUser.getUsername();
    }

    /**
     * 获取用户登录名
     *
     * @return username
     */
    public static SecurityUserVo getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null) {
            Object principal = authentication.getPrincipal();
            SecurityUserVo securityUserVo = null;
            if (principal instanceof SysUserDetails) {
                securityUserVo = ((SysUserDetails) principal).getUser();
            }
            return securityUserVo;
        }
        return null;
    }

    /**
     * 获取用户昵称/姓名
     *
     * @return realName
     */
    public static String getRealName() {
        SecurityUserVo currentUser = getCurrentUser();
        return Objects.isNull(currentUser) ? null : currentUser.getRealName();
    }

    public static Set<String> getRoles() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && CollectionUtil.isNotEmpty(authentication.getAuthorities())) {
            Set<String> roles = authentication.getAuthorities()
                    .stream()
                    .map(item -> StrUtil.removePrefix(item.getAuthority(), "ROLE_"))
                    .collect(Collectors.toSet());
            return roles;
        }
        return Collections.EMPTY_SET;
    }

    /**
     * 获取部门ID
     *
     * @return deptId
     */
    public static Long getDeptId() {
        SecurityUserVo currentUser = getCurrentUser();
        return Objects.isNull(currentUser) ? null : currentUser.getDeptId();
    }

    /**
     * 获取数据权限
     * @return
     */
    public static Integer getDataScope() {
        SecurityUserVo currentUser = getCurrentUser();
        return Objects.isNull(currentUser) ? null : currentUser.getDataScope();
    }

    /**
     * 判断用户是否为超级管理员
     *
     * @return true/false
     */
    public static boolean isAdmin() {
        return getRoles().contains(SecurityConstants.SYSTEM_ROLE_CODE);
    }
}
